//
//  CTVAutoUpdateController.m
//  Channltv
//
//  Created by Bruno Keymolen on 11/10/07.
//  Copyright 2007 Bruno Keymolen. All rights reserved.
//

#import "CTVAutoUpdateController.h"
#import "CTVConfig.h"
#import "CTVCache.h"


@implementation CTVAutoUpdateController

-(id)initWithScene:(BRRenderScene*)scene{
	self = [super initWithScene:scene];

	NSSize size = ([[self masterLayer] frame]).size;

	//Create the elements

	//Title
	m_titlecontrol = [[BRHeaderControl alloc] initWithScene:scene]; 
	[m_titlecontrol setTitle:@"Channl.TV Auto Update"]; 
	NSString* iconPath =  [[NSBundle bundleForClass: [self class]] pathForResource:@"channels" ofType: @"png"];
	BRTexture * tex = [CTVCache createBitmapTextureFromPath:iconPath withScene:[self scene]];	
	[m_titlecontrol setIcon:tex horizontalOffset:0.0f kerningFactor:0.1f];
	[self addControl:m_titlecontrol];


	//Info	
	m_infocontrol = [[BRTextControl alloc] initWithScene:scene];
	NSString* infoText = [NSString stringWithFormat:@"A new Update is available."];
	[m_infocontrol setText:infoText];
	[m_infocontrol setTextAttributes:[[BRThemeInfo sharedTheme] menuItemTextAttributes]];
	[self addControl:m_infocontrol];

	//Old version
	m_versionoldcontrol = [[BRTextControl alloc] initWithScene:scene];
	infoText = [NSString stringWithFormat:@"Installed version : %@", [[CTVConfig GetInstance] versionString] ];
	[m_versionoldcontrol setTextAttributes:[[BRThemeInfo sharedTheme] twoLineMenuItemTitleTextAttributes]];
	[self addControl:m_versionoldcontrol];
	[m_versionoldcontrol setText:infoText];
	
	//New version
	m_versionnewcontrol = [[BRTextControl alloc] initWithScene:scene];
	infoText = [NSString stringWithFormat:@"New version : %@", [[CTVAutoUpdate GetInstance] latestVersion] ];
	[m_versionnewcontrol setTextAttributes:[[BRThemeInfo sharedTheme] twoLineMenuItemTitleTextAttributes]];
	[self addControl:m_versionnewcontrol];
	[m_versionnewcontrol setText:infoText];

	//Spinner
	m_spinnercontrol =	[[BRWaitSpinnerControl alloc] initWithScene:scene];
	[m_spinnercontrol stopSpinning];
	[m_spinnercontrol setHidden:TRUE];
	[self addControl:m_spinnercontrol];



	//Button
	m_button = [[BRButtonControl alloc] initWithScene:scene masterLayerSize:size];
	[m_button setTarget:self];
	if([[CTVAutoUpdate GetInstance] isProcessing]){
		[m_button setTitle:@"Updating...."];	
		[m_button setAction:nil];
	}else{
		[m_button setTitle:@"Download and Update Now"];	
		[m_button setAction:@selector(updateNow)];
	}
	[self addControl:m_button];
	
	
	[self _layout];
	
	return self;
}

- (id)layer{
	return _master;
}


-(void)dealloc{
	[m_titlecontrol release];
	[m_versionoldcontrol release];
	[m_versionnewcontrol release];
	[m_infocontrol release];
	[m_spinnercontrol release];
	[m_button release];	
	
	[super dealloc];
}


-(void)updateNow{
	[m_spinnercontrol setHidden:FALSE];
	[m_spinnercontrol startSpinning];
	[m_button setTitle:@"Updating...."];	
	[m_button setAction:nil];
	[[self scene] renderScene];
	if([[CTVAutoUpdate GetInstance] isProcessing]){
		return;
	}
	[[CTVAutoUpdate GetInstance] updateNow];
}



//Controller will be displayed on screen 
- (void)willBePushed{
	[[CTVAutoUpdate GetInstance] addListener:(CTVAutoUpdateListener*)self];
	[super willBePushed];
}

//Controller will be removed from screen 
-(void)willBePopped{
	[[CTVAutoUpdate GetInstance] removeListener:(CTVAutoUpdateListener*)self];
	//Reset the flag, to avoid to many requests for update 
	[[CTVAutoUpdate GetInstance] resetUpdateAvailable];
	[super willBePopped];
}


-(void)_layout{
	
	NSRect frame = [[self masterLayer] frame];
	
	//Title, center, height = 10%
	NSRect titleFrame = NSZeroRect;
	titleFrame.origin.x = 0;
	titleFrame.origin.y = frame.size.height * 0.85f;
	titleFrame.size.width = frame.size.width; //titleTextSize.width;
	titleFrame.size.height = frame.size.height * 0.10f;
	[m_titlecontrol setFrame:titleFrame];
	
	
	//Info, width = 90%, height 50% (center)
	NSRect infoFrame = NSZeroRect;
	NSSize textSize = [m_infocontrol renderedSize];
	infoFrame.origin.x = (frame.size.width - textSize.width) * 0.5f; 
	infoFrame.origin.y = frame.size.height * 0.65f;
	infoFrame.size.width = textSize.width;
	infoFrame.size.height = frame.size.height * 0.10f;
	[m_infocontrol setFrame:infoFrame];
	
	
	//Version old
	NSRect versionOldFrame = infoFrame;
	textSize = [m_versionoldcontrol renderedSize];
	versionOldFrame.origin.x = (frame.size.width - textSize.width) * 0.5f; 
	versionOldFrame.size.width = textSize.width;
	versionOldFrame.origin.y = versionOldFrame.origin.y - versionOldFrame.size.height;
	[m_versionoldcontrol setFrame:versionOldFrame];


	//Version new
	NSRect versionNewFrame = versionOldFrame;
	textSize = [m_versionnewcontrol renderedSize];
	versionNewFrame.origin.x = (frame.size.width - textSize.width) * 0.5f; 
	versionNewFrame.size.width = textSize.width;
	versionNewFrame.origin.y = versionNewFrame.origin.y - (textSize.height * 1.25f);
	[m_versionnewcontrol setFrame:versionNewFrame];

			
	//Spinner (width = 10% of screen)
	NSRect spinnerFrame = versionNewFrame;
	spinnerFrame.size.width = frame.size.width * 0.10f;
	spinnerFrame.size.height = spinnerFrame.size.width;
	spinnerFrame.origin.y = spinnerFrame.origin.y - (spinnerFrame.size.height + textSize.height);
	spinnerFrame.origin.x = frame.size.width * 0.90f * 0.5f; 
	[m_spinnercontrol setFrame:spinnerFrame];

	    
	//Button, height 10%, width=20%, center
	NSRect buttonFrame = [m_button frame];
	buttonFrame.origin.y = frame.size.height * 0.05f;
	[m_button setFrame:buttonFrame];  
	
}



//CTVAutoUpdateListener
-(void)autoUpdateCheckDone:(NSString*)version{
	[m_spinnercontrol stopSpinning];
}

-(void)autoUpdateCheckFail{
	[m_spinnercontrol stopSpinning];
	[m_button setTitle:@"Update Failed (!)"];	
	[m_button setAction:nil];
	[[self scene] renderScene];
}


@end
